<template>
    <div class="bg" v-if="show">
        <el-button @click="close" class="close">关闭</el-button>
        <div class="canvas-container">
            <div class="canvas" :style="{
                    width: changeStyleWithScale(canvasStyleData.width) + 'px',
                    height: changeStyleWithScale(canvasStyleData.height) + 'px',
                }">
                <ComponentWrapper v-for="(item, index) in componentData" :key="index" :config="item" />
                <div id="player" ref="player" :style="{
                    width: changeStyleWithScale(canvasStyleData.width) + 'px',
                    height: changeStyleWithScale(canvasStyleData.height) + 'px',
                }"></div>
            </div>
        </div>
    </div>
</template>

<script>
import { getStyle } from '@/utils/style'
import { mapState } from 'vuex'
import ComponentWrapper from './ComponentWrapper'
import { changeStyleWithScale } from '@/utils/translate'

export default {
    model: {
        prop: 'show',
        event: 'change',
    },
    props: {
        show: {
            type: Boolean,
            default: false,
            aircityPlayer: null,
            aircityApi: null,
        },
    },
    components: { ComponentWrapper },
    computed: mapState(['componentData', 'canvasStyleData']),
    mounted() {},
    watch: {},
    destroyed() {},
    methods: {
        changeStyleWithScale,

        getStyle,

        close() {
            this.$emit('change', false)
            if (__g) {
                this.$store.state.airCityPlayer.destroy()
                __g.destroy()
            }
            this.$store.commit('setairCityPlayer', null)
            this.$store.commit('setaircityApi', null)
            if (this.$store.state.CurrentInstanceId) {
                let options = {
                    domId: 'editorPlayer',
                    token: HostConfig.Token,
                    showMarker: true,
                    showStartupInfo: true,
                }
                let instanceId = this.$store.state.CurrentInstanceId
                console.log(instanceId)
                setTimeout(() => {
                    this.aircityPlayer = new AirCityPlayer(instanceId, options)
                    this.$store.commit('setairCityPlayer', this.aircityPlayer)
                    this.aircityApi = new AirCityAPI(instanceId, {
                        onReady: () => {},
                        onApiVersion: (vNo, vType) => {},
                        onEvent: e => {
                            console.log(e)
                            this.$store.commit('saveOnEvent', e)
                        },
                        onLog: e => {},
                        useColorLog: false, //仅用于SDK测试页面，二次开发请设置为false
                    })
                    this.$store.commit('setaircityApi', this.aircityApi)
                    console.log(instanceId)
                }, 1000)
            }
        },
    },
}
</script>

<style lang="scss" scoped>
.bg {
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    position: fixed;
    background: rgb(0, 0, 0, 0.5);
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: auto;
    position: absolute;

    .canvas-container {
        background: #000000ab;
        overflow: auto;
        width: 100%;
        height: 100%;
        .canvas {
            // background: #fff;
            width: calc(100%);
            height: calc(100%);
            position: relative;
            margin: auto;
        }
    }

    .close {
        position: absolute;
        right: 20px;
        top: 20px;
        z-index: 999999;
    }
}
#player {
    width: 100%;
    height: 100%;
    position: absolute;
}
</style>